/**
 * 工具
 */
export class Tool {
    /**
     * 随机数
     * @param min
     * @param max
     * @returns {number}
     */
    static random(min, max){
        max = max?max:9999999;
        min = min?min:1000000;
        return Math.ceil(Math.random() * (max - min + 1) + min);
    }

    /**
     *
     * @param len
     * @returns {string}
     */
    static randomLetter(len){
        len = len || 1;
        let chars = 'abcdefghijklmnopqrstuvwxyz';
        let maxPos = chars.length;
        let str = '';
        for (let i = 0; i < len; i++) {
            str += chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return str;
    }
    /**
     *
     * @param len
     * @returns {string}
     */
    static randomLetterNumber(len){
        len = len || 1;
        let chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
        let maxPos = chars.length;
        let str = '';
        for (let i = 0; i < len; i++) {
            str += chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return str;
    }

    /**
     * 返回当前时间戳
     * @returns {number}
     */
    static getNowTime(){
        let d = new Date();
        return d.getTime();
    }

    /**
     * 将对象转换为类的示例
     * @returns {[]}
     */
    static elementToClassObject(elements, object){
        let toObjs = [];
        elements.each(function (a, ele) {
            let myFoe = new object();
            let obj = Tool.elementObject(ele);
            for(let k in obj){
                myFoe[k] = obj[k];
            }
            toObjs.push(myFoe);
        });
        return toObjs;
    }
    /**
     * 将元素属性转换为对象属性
     * @param element
     * @returns {{}}
     */
    static elementObject(element){
        let obj = {};
        $(element).each(function() {
            $.each(this.attributes, function() {
                // this.attributes is not a plain object, but an array
                // of attribute nodes, which contain both the name and value
                if(this.specified) {
                    obj[this.name] = this.value;
                }
            });
        });
        return obj;
    }
}